/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui.view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.connect5.relations.Equal;
import org.jfonia.constants.ModelConstants;
import org.jfonia.constants.SymbolConstants;
import org.jfonia.model.QuantizerNode;
import org.jfonia.model.TempoNode;
import org.jfonia.model.elements.TimeSignature;
import percussiongenerator.gui.ColorChooserLabel;
import percussiongenerator.gui.LengthPanel;
import percussiongenerator.gui.QuantizerPanel;
import percussiongenerator.gui.TimeTextField;
import percussiongenerator.gui.view.TimeLineView.Grid;

/**
 *
 * @author Jannes Plyson
 */
public class TimeLineViewOptions extends ViewOptionsPanel implements Observer{
    TimeLineView timeLineView;
    QuantizerPanel quantizerPanel;
    ArrayList<Equal<Color>> colorRelations;
    Box gridColorBox;
    TimeTextField ttfMousePointer;

    public TimeLineViewOptions(TimeLineView timeLineView){
        this.timeLineView = timeLineView;
        colorRelations = new ArrayList<Equal<Color>>();
        Box vBox = Box.createVerticalBox();
        this.add(vBox);
        Box hBox = Box.createHorizontalBox();
        hBox.add(new JLabel("Time: "));
        ttfMousePointer = new TimeTextField(0l);
        hBox.add(ttfMousePointer);
        hBox.add(Box.createHorizontalGlue());
        vBox.add(hBox);
        timeLineView.getTimeNode().addObserver(this);
        LengthPanel lengthPanel = new LengthPanel(timeLineView.track);
        vBox.add(lengthPanel);        

        gridColorBox = Box.createVerticalBox();
        vBox.add(gridColorBox);
        createGridPanel();

        QuantizerNode quantizerNode = timeLineView.track.getStaff().getQuantizerNode();
        TempoNode tempoNode = (TempoNode)timeLineView.track.getStaff().getTempoMap().getIntervalFromIndex(0);
        quantizerPanel = new QuantizerPanel(quantizerNode,tempoNode);
        hBox = Box.createHorizontalBox();
        vBox.add(hBox);
        hBox.add(quantizerPanel);
        hBox.add(Box.createHorizontalGlue());
    }

    public void createGridPanel(){
        for(Equal eq : colorRelations){
            eq.switchOff();
        }
        gridColorBox.removeAll();
        final Grid grid = timeLineView.getGrid();
        Box hBox = Box.createHorizontalBox();        
        ColorChooserLabel lblcc = new ColorChooserLabel(grid.gridColor.get(0).getValue());
        colorRelations.add(new Equal<Color>(lblcc.getColorNode(),grid.gridColor.get(0)));
        lblcc.getColorNode().addObserver(this);
        hBox.add(lblcc);
        hBox.add(new JLabel(" :beat color"));
        hBox.add(Box.createHorizontalGlue());
        gridColorBox.add(hBox);

        hBox = Box.createHorizontalBox();        
        lblcc = new ColorChooserLabel(grid.gridColor.get(1).getValue());
        colorRelations.add(new Equal<Color>(lblcc.getColorNode(),grid.gridColor.get(1)));
        lblcc.getColorNode().addObserver(this);
        hBox.add(lblcc);
        hBox.add(new JLabel(" :measure color"));
        hBox.add(Box.createHorizontalGlue());
        gridColorBox.add(hBox);

        for(int i = 0; i < grid.gridWidth.size(); i++){
            hBox = Box.createHorizontalBox();            
            final ColorChooserLabel lblcc2 = new ColorChooserLabel(grid.gridColor.get(i+grid.measureWidth.size()).getValue());
            final Equal<Color> relation = new Equal<Color>(lblcc2.getColorNode(),grid.gridColor.get(i+grid.measureWidth.size()));
            colorRelations.add(relation);
            lblcc2.getColorNode().addObserver(this);
            hBox.add(lblcc2);
            hBox.add(new JLabel(" :" + grid.gridWidth.get(i)));
            JButton btnRemove = new JButton("X");
            final TimeLineViewOptions thisObject = this;
            final int index = i;
            btnRemove.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    relation.switchOff();
                    colorRelations.remove(relation);
                    lblcc2.getColorNode().removeObserver(thisObject);
                    grid.gridWidth.remove(index);
                    createGridPanel();
                    timeLineView.repaint();
                }
            });
            hBox.add(btnRemove);
            hBox.add(Box.createHorizontalGlue());
            gridColorBox.add(hBox);
        }

        hBox = Box.createHorizontalBox();
        final JTextField txtAddGrid = new JTextField();
        hBox.add(txtAddGrid);
        JButton btnAddGrid = new JButton("add grid");
        btnAddGrid.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 try{
                    String text = txtAddGrid.getText();
                    Long step = 0l;
                    if(text.endsWith("mic")){
                        step = Long.parseLong(text.substring(0,text.length()-3))*1000l;
                    }else if(text.endsWith("m")){
                        step = Long.parseLong(text.substring(0,text.length()-1))*1000000l;
                    }else if(text.endsWith("t") || text.endsWith("n")){
                        int ticks = 1;
                        if(text.endsWith("n")){
                            int noteValue = Integer.parseInt(text.substring(0,text.length()-1));
                            ticks = SymbolConstants.DIVISOR/noteValue;
                        }else{
                            ticks = Integer.parseInt(text.substring(0,text.length()-1));
                        }
                        int bpm = ((TempoNode)timeLineView.track.getStaff().getTempoMap().getIntervalFromValue(ticks)).getBPM();
                        int beatsize = ((TimeSignature)(timeLineView.track.getStaff().getSequence(TimeSignature.class).getIntervalFromValue(ticks))).getDenominator();
                        int beatsizeTicks = SymbolConstants.DIVISOR/beatsize;
                        step = (60l*ModelConstants.PERFORMANCE_TO_SECOND*ticks)/(bpm*beatsizeTicks);
                    }else{
                        step = Long.parseLong(text);
                    }
                    grid.add(step);
                    createGridPanel();
                    timeLineView.repaint();
                }catch(Exception exc){}
            }
        });
        hBox.add(btnAddGrid);
        hBox.add(Box.createHorizontalGlue());
        gridColorBox.add(hBox);
        repaint();
        revalidate();
    }

    public void removeRelations(){
        quantizerPanel.removeObservers();
        for(Equal eq : colorRelations){
            eq.switchOff();
        }
        timeLineView.getTimeNode().removeObserver(this);
    }

    public void onNotify(Object source) {        
        if(source == timeLineView.getTimeNode()){
            ttfMousePointer.setTime(timeLineView.getTimeNode().getValue());
        }
        timeLineView.repaint();
    }
}
